package edu.usc.csci578.model;

import java.util.ArrayList;
import java.util.List;

public class SMethod {

	public static final String XML_PUBLIC = "public";
	public static final String XML_PROTECTED = "protected";
	public static final String XML_PRIVATE = "private";

	public static final Integer METHOD_TYPE_PUBLIC = 1000;
	public static final Integer METHOD_TYPE_PROTECTED = 1001;
	public static final Integer METHOD_TYPE_PRIVATE = 1002;

	private String name = null;
	private Integer type = 0;
	private SClass owner = null;
	private List<SClass> paras = null;
	private SClass retval = null;

	public SMethod(String name, Integer type, SClass owner) {
		init(name, type, owner, null, null);
	}

	public SMethod(String name, Integer type, SClass owner, List<SClass> paras,
			SClass retval) {
		init(name, type, owner, paras, retval);
	}

	private void init(String name, Integer type, SClass owner,
			List<SClass> paras, SClass retval) {
		this.name = name;
		this.type = type;
		this.owner = owner;
		this.paras = paras;
		this.retval = retval;
		if (paras == null) {
			this.paras = new ArrayList<SClass>();
		}
	}

	public boolean addParas(SClass para) {
		if (para != null && !paras.contains(para)) {
			paras.add(para);
			return true;
		}
		return false;
	}

	public void setRetval(SClass retval) {
		this.retval = retval;
	}

	public String getName() {
		return name;
	}

	public String getUniqueName() {
		return owner.getPackageName() + "." + owner.getName() + "." + name;
	}

	public boolean isPublic() {
		return type == METHOD_TYPE_PUBLIC;
	}

	public boolean isProtected() {
		return type == METHOD_TYPE_PROTECTED;
	}

	public boolean isPrivate() {
		return type == METHOD_TYPE_PRIVATE;
	}

	public SClass getOwner() {
		return owner;
	}

	public List<SClass> getParas() {
		return paras;
	}

	public SClass getRetval() {
		return retval;
	}

	public String toString() {
		StringBuffer output = new StringBuffer();
		output.append("method name: ");
		output.append(this.name);
		output.append("\nparas:\n");
		for (SClass para : paras) {
			output.append(para.getUniqueName());
			output.append("\n");
		}
		output.append("retval: ");
		output.append(retval.getUniqueName());
		output.append("\n");
		return output.toString();
	}

}
